package com.miaoxie.forum.module.auth.shared.utils;

import com.miaoxie.forum.module.auth.shared.exception.PropertiesLoadException;

import java.io.*;
import java.util.HashMap;

/**
 * Created by miaoxie on 2017/3/02.
 */
public class Properties extends HashMap<String, String> {

    private static final long serialVersionUID = 6844431364587351965L;
    //设置编码格式为UTF-8
    private static final String CHARSET_NAME = "UTF-8";

    public Properties() {
        super();
    }

    /**
     * <p>合并Map</p>
     * @author 平台开发小组
     * @date 2013-1-21 上午9:03:20
     * #see
     */
    public Properties(Properties defaults) {
        putAll(defaults);
    }


    /**
     * <p>通过key获取value</p>
     * @author 平台开发小组
     * @date 2013-1-21 上午9:03:20
     * #see
     */
    public String getProperty(String key) {
        return get(key);
    }

    /**
     * <p>通过key获取value 如果value为空为默认值</p>
     * @author 平台开发小组
     * @date 2013-1-21 上午9:03:20
     * #see
     */
    public String getProperty(String key, String defaultValue) {
        String val = getProperty(key);
        return (val == null) ? defaultValue : val;
    }

    /**
     * <p>加载Reader字符流</p>
     * @author 平台开发小组
     * @date 2013-1-21 上午9:03:20
     * #see
     */
    public void load(Reader reader) throws IOException {
        loadInputStream(new Properties.ReaderInputStream(reader));
    }

    /**
     * <p>加载InputStream输入流</p>
     * @author 平台开发小组
     * @date 2013-1-21 上午9:03:20
     * #see
     */
    public synchronized void load(InputStream inStream) throws IOException {
        loadInputStream(new Properties.ReaderInputStream(inStream));
    }

    private void loadInputStream(Properties.ReaderInputStream lr) throws IOException {
        String line;
        //从输入流读取字节流
        BufferedReader bufferedReader = lr.reader();
        int count = 0;
        while (null != (line = bufferedReader.readLine())) {
            count++;
            line = line.trim();
            //
            if (/* null==line|| */line.length() == 0) {
                continue;
            }
            //如果是以#开头则不向下执行
            if (line.startsWith("#")) {
                continue;
            }
            String[] strs = line.split("=", 2);
            //判断数组是长度是否为2，是否符合map的key value
            if (strs.length != 2) {
                throw new PropertiesLoadException("load properties error,line " + count + ":\"" + line
                        + "\" config error");
            }
            //将数组第一个值作为key
            String key = strs[0].trim();
            //将数组第二个值作为value
            String value = strs[1].trim();
            put(key, value);
        }
    }

    static class ReaderInputStream {

        InputStream inStream;
        Reader reader;
        BufferedReader bufferedReader;

        public ReaderInputStream(InputStream inStream) {
            this.inStream = inStream;
        }

        public ReaderInputStream(Reader reader) {
            this.reader = reader;
        }

        BufferedReader getBufferedReader() {
            return bufferedReader;
        }

        BufferedReader reader() throws IOException {
            //如果inStream不为空，进行字节流读取 否则进行字符流读取
            if (null != inStream) {
                bufferedReader = new BufferedReader(new InputStreamReader(inStream, CHARSET_NAME));
            } else {
                bufferedReader = new BufferedReader(reader);
            }
            return bufferedReader;
        }
    }
}
